package com.huixuebao.after.controller.course;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.huixuebao.after.dto.*;
import com.huixuebao.after.entity.CategoryEntity;
import com.huixuebao.after.service.CourseService;
import com.huixuebao.after.util.BizLicenseOCR;
import com.huixuebao.after.util.MFileUtil;
import com.huixuebao.after.util.OSSClientUtil;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

@Api(value = "课程模块",tags = "{课程模块}")
@Slf4j
@RestController
@RequestMapping("/hxb/after/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 获取课程list
     * @param current
     * @param size
     * @param courseName
     * @param mechanismName
     * @param courseState
     * @return
     */
    @ApiOperation("getCourseList")
    @GetMapping("/getListCourse")
    public ApiResult getListCourse(Integer current,Integer size,String courseName,String mechanismName,String courseState,HttpServletRequest request){
        return courseService.getListCourse(current,size,courseName,mechanismName,courseState,request);
    }

    /**
     * 添加课程
     * @param courseListDTO
     * @return
     */
    @PostMapping("/addCourse")
    public ApiResult addCourse(@RequestBody CourseListDTO courseListDTO,HttpServletRequest request){
        return courseService.addCourse(courseListDTO,request);
    }

    /**
     * 编辑课程
     * @param courseListDTO
     * @return
     */
    @PostMapping("/editCourse")
    public ApiResult editCourse(@RequestBody CourseListDTO courseListDTO){
        return courseService.editCourse(courseListDTO);
    }

    /**
     * 上传图片到阿里服务器上OSS上，将路径返回给前端
     * 点击图片选择图片后，传输到服务器中。
     */
    @PostMapping("/uploadImage")
    public ApiResult uploadImage(@RequestParam("file") MultipartFile file){
        ApiResult apiResult = new ApiResult();
        ImagePathDTO imagePathDTO = new ImagePathDTO();
        try {
            String imageFile = file.getOriginalFilename();
            File tempFile = File.createTempFile(IdUtil.simpleUUID(),"." + MFileUtil.getExp(imageFile));
            file.transferTo(tempFile);
            String url = OSSClientUtil.uploadObject2OSS(tempFile);
            imagePathDTO.setImagePath(url);
            if (StrUtil.isNotEmpty(url)){
                apiResult.setData(imagePathDTO);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("保存课程.Jpg成功！！");
            }else {
                apiResult.setSuccess(false);
                apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
                apiResult.setMessage("上传图片失败!!");
            }
        }catch (Exception e){
            log.info("保存图片异常");
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("保存.Jpg失败！！");
        }
        return apiResult;
    }
    /**
     * 获取课程类目树信息
     */
    @GetMapping("/categoryTree")
    public ApiResult<List<CategoryAfterDTO>> categoryTree(){
        return courseService.categoryTree();
    }
    /**
     * 修改课程状态
     */
    @PostMapping("/updateCourseState")
    public ApiResult updateCourseState(@RequestBody CourseIdDTO courseIdDTO){
        return courseService.updateCourseState(courseIdDTO);
    }

    /**
     * 返回Course信息
     * @param courseIdDTO
     * @return
     */
    @GetMapping("/retContent")
    public ApiResult retContent(CourseIdDTO courseIdDTO){
        return courseService.retContent(courseIdDTO);
    }
}
